/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.statweb.common;

import cn.ac.iie.di.wx.statweb.configuration.Configuration;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String MSG_URL = "msg_url";
    public static final String USER_URL = "user_url";
    public static final String USER = "user";
    public static final String PASSWORD = "password";
    public static final String REDIS_MASTER_NAME = "redis.master.name";
    public static final String REDIS_HOSTS = "redis.hosts";
    public static final String REDIS_DB_NAME = "redis.db.name";
    public static final String MASSALL = "massAll";
    public static final String MASSSUC = "massSuc";
    public static final String SINGLEALL = "singleAll";
    public static final String SINGLESUC = "singleSuc";
    public static final String DATASIZE = "dataSize";
    public static final String ALLCOUNT = "allCount";
    public static final String SUCCESSCOUNT = "successCount";
    public static final String EVALUATE = "evaluate";
    
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        String msg_url = conf.getString(MSG_URL, "");
        if (MSG_URL.isEmpty()) {
            logger.error("parameter msg_url does not exist or is not defined");
            throw new Exception("parameter msg_url does not exist or is not defined");
        }
        addParam(MSG_URL, msg_url);

        String user_url = conf.getString(USER_URL, "");
        if (USER_URL.isEmpty()) {
            logger.error("parameter user_url does not exist or is not defined");
            throw new Exception("parameter user_url does not exist or is not defined");
        }
        addParam(USER_URL, user_url);

        String user = conf.getString(USER, "");
        if (USER.isEmpty()) {
            logger.error("parameter user does not exist or is not defined");
            throw new Exception("parameter user does not exist or is not defined");
        }
        addParam(USER, user);

        String password = conf.getString(PASSWORD, "");
        if (PASSWORD.isEmpty()) {
            logger.error("parameter password does not exist or is not defined");
            throw new Exception("parameter password does not exist or is not defined");
        }
        addParam(PASSWORD, password);

        String redis_master_name = conf.getString(REDIS_MASTER_NAME, "");
        if (REDIS_MASTER_NAME.isEmpty()) {
            logger.error("parameter redis_master_name does not exist or is not defined");
            throw new Exception("parameter redis_master_name does not exist or is not defined");
        }
        addParam(REDIS_MASTER_NAME, redis_master_name);

        String redis_hosts = conf.getString(REDIS_HOSTS, "");
        if (REDIS_HOSTS.isEmpty()) {
            logger.error("parameter redis_hosts does not exist or is not defined");
            throw new Exception("parameter redis_hosts does not exist or is not defined");
        }
        addParam(REDIS_HOSTS, redis_hosts);

        String redis_db_name = conf.getString(REDIS_DB_NAME, "");
        if (REDIS_DB_NAME.isEmpty()) {
            logger.error("parameter redis_db_name does not exist or is not defined");
            throw new Exception("parameter redis_db_name does not exist or is not defined");
        }
        addParam(REDIS_DB_NAME, redis_db_name);

        String massAll = conf.getString(MASSALL, "");
        if (MASSALL.isEmpty()) {
            logger.error("parameter massAll does not exist or is not defined");
            throw new Exception("parameter massAll does not exist or is not defined");
        }
        addParam(MASSALL, massAll);

        String massSuc = conf.getString(MASSSUC, "");
        if (MASSSUC.isEmpty()) {
            logger.error("parameter massSuc does not exist or is not defined");
            throw new Exception("parameter massSuc does not exist or is not defined");
        }
        addParam(MASSSUC, massSuc);

        String singleAll = conf.getString(SINGLEALL, "");
        if (SINGLEALL.isEmpty()) {
            logger.error("parameter singleAll does not exist or is not defined");
            throw new Exception("parameter singleAll does not exist or is not defined");
        }
        addParam(SINGLEALL, singleAll);
        
        String singleSuc = conf.getString(SINGLESUC, "");
        if (SINGLESUC.isEmpty()) {
            logger.error("parameter singleSuc does not exist or is not defined");
            throw new Exception("parameter singleSuc does not exist or is not defined");
        }
        addParam(SINGLESUC, singleSuc);
        
        String dataSize=conf.getString(DATASIZE, "");
        if(DATASIZE.isEmpty()){
            logger.error("parameter dataSize does not exist or is not defined");
            throw new Exception("parameter dataSize does not exist or is not defined");
        }
        addParam(DATASIZE, dataSize);
        
        String allCount=conf.getString(ALLCOUNT, "");
        if(ALLCOUNT.isEmpty()){
            logger.error("parameter allCount does not exist or is not defined");
            throw new Exception("parameter allCount does not exist or is not defined");
        }
        addParam(ALLCOUNT, allCount);
        
        String successCount=conf.getString(SUCCESSCOUNT, "");
        if(SUCCESSCOUNT.isEmpty()){
            logger.error("parameter successCount does not exist or is not defined");
            throw new Exception("parameter successCount does not exist or is not defined");
        }
        addParam(SUCCESSCOUNT, successCount);
        
        String evaluate=conf.getString(EVALUATE, "");
        if(EVALUATE.isEmpty()){
            logger.error("parameter evaluate does not exist or is not defined");
            throw new Exception("parameter evaluate does not exist or is not defined");
        }
        addParam(EVALUATE, evaluate);
        
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
